home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / packer.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  119 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *      control bytes:
  8.  *       [0..127]   : followed by n+1 bytes of data.
  9.  *       [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *       -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "packer.h"
  15.  
  16. #define DUMP    0
  17. #define RUN     1
  18.  
  19. #define MinRun 3
  20. #define MaxRun 128
  21. #define MaxDat 128
  22.  
  23. LONG putSize;
  24. #define GetByte()       (*source++)
  25. #define PutByte(c)      { *dest++ = (c);   ++putSize; }
  26.  
  27. char buf[256];  /* [TBD] should be 128?  on stack?*/
  28.  
  29. BYTE *PutDump(dest, nn)
  30. BYTE *dest;
  31. int nn;
  32. {
  33.         int i;
  34.  
  35.         PutByte(nn-1);
  36.         for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  37.         return(dest);
  38. }
  39.  
  40. BYTE *PutRun(dest, nn, cc)
  41. BYTE *dest;
  42. int nn, cc;
  43. {
  44.         PutByte(-(nn-1));
  45.         PutByte(cc);
  46.         return(dest);
  47. }
  48.  
  49. #define OutDump(nn)   dest = PutDump(dest, nn)
  50. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  51.  
  52. /*----------- PackRow --------------------------------------------------*/
  53. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  54.    destination pointers.  RETURNs count of packed bytes.*/
  55.  
  56. LONG PackRow(pSource, pDest, rowSize)
  57. BYTE **pSource, **pDest;
  58. LONG rowSize;
  59. {
  60.     BYTE *source, *dest;
  61.     char c,lastc = '\0';
  62.     BOOL mode = DUMP;
  63.     short nbuf = 0;             /* number of chars in buffer */
  64.     short rstart = 0;           /* buffer index current run starts */
  65.  
  66.     source = *pSource;
  67.     dest = *pDest;
  68.     putSize = 0;
  69.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  70.     nbuf = 1;   rowSize--;      /* since one byte eaten.*/
  71.  
  72.  
  73.     for (;  rowSize;  --rowSize) {
  74.         buf[nbuf++] = c = GetByte();
  75.         switch (mode) {
  76.                 case DUMP:
  77.                         /* If the buffer is full, write the length byte,
  78.                            then the data */
  79.                         if (nbuf>MaxDat) {
  80.                                 OutDump(nbuf-1);
  81.                                 buf[0] = c;
  82.                                 nbuf = 1;   rstart = 0;
  83.                                 break;
  84.                                 }
  85.  
  86.                         if (c == lastc) {
  87.                             if (nbuf-rstart >= MinRun) {
  88.                                 if (rstart > 0) OutDump(rstart);
  89.                                 mode = RUN;
  90.                                 }
  91.                             else if (rstart == 0)
  92.                                 mode = RUN;     /* no dump in progress,
  93.                                 so can't lose by making these 2 a run.*/
  94.                             }
  95.                         else  rstart = nbuf-1;          /* first of run */
  96.                         break;
  97.  
  98.                 case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  99.                         /* output run */
  100.                         OutRun(nbuf-1-rstart,lastc);
  101.                         buf[0] = c;
  102.                         nbuf = 1; rstart = 0;
  103.                         mode = DUMP;
  104.                         }
  105.                         break;
  106.                 }
  107.  
  108.         lastc = c;
  109.         }
  110.  
  111.     switch (mode) {
  112.         case DUMP: OutDump(nbuf); break;
  113.         case RUN: OutRun(nbuf-rstart,lastc); break;
  114.         }
  115.     *pSource = source;
  116.     *pDest = dest;
  117.     return(putSize);
  118. }
  119.